package org.abettor.nookinfo.ads;

import org.abettor.nookinfo.R;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import net.youmi.android.AdManager;
import net.youmi.android.AdView;

/**
 * “有米”广告工具类
 * @author shawn
 *
 */
public class YoumiManager
{
	private Activity activity;
	private RelativeLayout container;
	private AdView ads;
	static
	{
		AdManager.init("752995b1613b5ad8", "c060df3d524f6054", 20, false);
	}

	public YoumiManager(Activity activity)
	{
		this.activity = activity;
	}

	/**
	 * 显示广告
	 */
	public void showYoumi(Position position, int backgroundColor, int textColor, int backgroundTransparent)
	{
		if(container == null)
		{
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
			container = new RelativeLayout(activity);
			container.setLayoutParams(params);
			container.setBackgroundColor(Color.TRANSPARENT);
			activity.addContentView(container, params);
		}

		if(ads != null)
		{
			return;
		}

		int alignRule = 0;
		switch(position)
		{
		case top:
			alignRule = RelativeLayout.ALIGN_PARENT_TOP;
			break;
		case middle:
			alignRule = RelativeLayout.CENTER_VERTICAL;
			break;
		case bottom:
			alignRule = RelativeLayout.ALIGN_PARENT_BOTTOM;
			break;
		}

		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(alignRule);
		ads = new AdView(activity, backgroundColor, textColor, backgroundTransparent);
		ads.setLayoutParams(params);
		container.addView(ads);

		params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		ImageView img = new ImageView(activity);
		img.setScaleType(ImageView.ScaleType.FIT_XY);
		img.setLayoutParams(params);
		img.setImageResource(R.drawable.ads_button_close);
		img.setClickable(true);
		img.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				YoumiManager.this.hideYoumi();
			}
		});
		ads.addView(img);
	}

	/**
	 * 隐藏广告
	 */
	public void hideYoumi()
	{
		if(ads == null || container == null)
		{
			return;
		}

		container.removeAllViews();
		ads = null;
	}

	/**
	 * 广告显示的位置
	 * @author shawn
	 *
	 */
	public enum Position
	{
		top,
		middle,
		bottom;
	}
}
